package com.sdhs.paas.itims.app;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.TypeReference;
import com.sdhs.paas.itims.bean.ItimsBusinessBean;
import com.sdhs.paas.itims.dto.ItimsBusinessDTO;
import com.sdhs.paas.itims.dto.req.ItimsBusinessReqDTO;
import com.sdhs.paas.itims.dto.res.ItimsBusinessResDTO;
import com.sdhs.paas.itims.service.ItimsBusinessService;
import com.sdhs.paas.sdk.base.common.BasePage;
import com.sdhs.paas.sdk.base.dto.PageInfoDTO;
import com.sdhs.paas.sdk.base.page.Page;
import com.sdhs.paas.sdk.base.utils.EntityDtoConvertUtil;
import com.sdhs.paas.sdk.core.PaasBaseRequest;
import com.sdhs.paas.sdk.core.PaasBaseResponse;
import com.sdhs.paas.sdk.core.RefreshableRestController;

/**
 * @author ItimsBusiness
 * @version 1.0
 * @Title:  代码生成接口，用于接受前端传输过来的数据
 * @author kangjichang
 * @date 2018-09-18
 */
 
@RefreshableRestController
@RequestMapping(value = "/itimsBusiness")
public class ItimsBusinessController {

    @Autowired
    private ItimsBusinessService itimsBusinessService;
    
    /**
     * 分页查询接口
     * */
    @PostMapping("/getItimsBusinessPageList")
	public PaasBaseResponse<ItimsBusinessResDTO> getItimsBusinessPageList(@RequestBody PaasBaseRequest<ItimsBusinessReqDTO> itimsBusinessReqDTO) {
    	ItimsBusinessDTO itimsBusinessDTO = itimsBusinessReqDTO.getBody().getItimsBusinessDTO();
    	PageInfoDTO pageInfoDTO = itimsBusinessReqDTO.getBody().getPageInfo();
    	
		BasePage pageRequest = new BasePage();
		pageRequest.setPage(pageInfoDTO.getPage());
		pageRequest.setCount(pageInfoDTO.getPageSize());
    	
    	ItimsBusinessBean itimsBusinessBean = EntityDtoConvertUtil.convertToEntity(itimsBusinessDTO, ItimsBusinessBean.class);
    	Page<ItimsBusinessBean> page = itimsBusinessService.getPageItimsBusinessList(pageRequest, itimsBusinessBean);
    	PageInfoDTO resPageInfoDTO = new PageInfoDTO();
    	ItimsBusinessResDTO itimsBusinessResDTO = new ItimsBusinessResDTO();
    	resPageInfoDTO.setPage(page.getCurPage());
		resPageInfoDTO.setPageSize(page.getPageSize());
		resPageInfoDTO.setPageCount(page.getPageCount());
		resPageInfoDTO.setRowCount(page.getTotalCount());
		itimsBusinessResDTO.setPageInfo(resPageInfoDTO);
		List<ItimsBusinessBean> ucAppBeanList = page.getResult();
		List<ItimsBusinessDTO> dtoList = new ArrayList<ItimsBusinessDTO>();
		dtoList = EntityDtoConvertUtil.convertToDTOList(ucAppBeanList, new TypeReference<List<ItimsBusinessDTO>>() {
		});
		itimsBusinessResDTO.setItimsBusinessDTOList(dtoList);
    	return new PaasBaseResponse<>(itimsBusinessResDTO);
	}
    
    /**
     * 分页查询接口
     * */
    @PostMapping("/getItimsBusinessList")
	public PaasBaseResponse<ItimsBusinessResDTO> getItimsBusinessList(@RequestBody PaasBaseRequest<ItimsBusinessReqDTO> itimsBusinessReqDTO) {
    	ItimsBusinessDTO itimsBusinessDTO = itimsBusinessReqDTO.getBody().getItimsBusinessDTO();
    	ItimsBusinessBean itimsBusinessBean = EntityDtoConvertUtil.convertToEntity(itimsBusinessDTO, ItimsBusinessBean.class);
    	List<ItimsBusinessBean> businessList = itimsBusinessService.getList(itimsBusinessBean);
    	List<ItimsBusinessDTO> dtoList = new ArrayList<ItimsBusinessDTO>();
		dtoList = EntityDtoConvertUtil.convertToDTOList(businessList, new TypeReference<List<ItimsBusinessDTO>>() {
		});
		ItimsBusinessResDTO itimsBusinessResDTO = new ItimsBusinessResDTO();
		itimsBusinessResDTO.setItimsBusinessDTOList(dtoList);
    	return new PaasBaseResponse<>(itimsBusinessResDTO);
    }
	
	/**
	 * 保存接口
	 * */
	@PostMapping("/saveItimsBusiness")
	public PaasBaseResponse<ItimsBusinessResDTO> saveItimsBusiness(@RequestBody PaasBaseRequest<ItimsBusinessReqDTO> itimsBusinessReqDTO) {
		ItimsBusinessDTO itimsBusinessDTO = itimsBusinessReqDTO.getBody().getItimsBusinessDTO();
		ItimsBusinessBean itimsBusinessBean = EntityDtoConvertUtil.convertToEntity(itimsBusinessDTO, ItimsBusinessBean.class);
		String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
		itimsBusinessBean.setId(uuid);
		itimsBusinessBean.setIsDeleted(0);
		Boolean flag = itimsBusinessService.saveItimsBusiness(itimsBusinessBean);
		itimsBusinessDTO.setId(uuid);
		ItimsBusinessResDTO itimsBusinessResDTO = new ItimsBusinessResDTO();
		itimsBusinessResDTO.setItimsBusinessDTO(itimsBusinessDTO);
		return new PaasBaseResponse<>(itimsBusinessResDTO);
	}
	
	/**
	 * 修改接口
	 * */
	@PostMapping("/updateItimsBusiness")
	public PaasBaseResponse<ItimsBusinessResDTO> updateItimsBusiness(@RequestBody PaasBaseRequest<ItimsBusinessReqDTO> itimsBusinessReqDTO) {
		ItimsBusinessDTO itimsBusinessDTO = itimsBusinessReqDTO.getBody().getItimsBusinessDTO();
		ItimsBusinessBean itimsBusinessBean = EntityDtoConvertUtil.convertToEntity(itimsBusinessDTO, ItimsBusinessBean.class);
		Boolean flag = itimsBusinessService.updateItimsBusiness(itimsBusinessBean);
		ItimsBusinessResDTO itimsBusinessResDTO = new ItimsBusinessResDTO();
		return new PaasBaseResponse<>(itimsBusinessResDTO);
	}
	
	/**
	 * 获取详情接口
	 * */
	@PostMapping("/getItimsBusinessDetail")
	public PaasBaseResponse<ItimsBusinessResDTO> getItimsBusinessDetail(@RequestBody PaasBaseRequest<ItimsBusinessReqDTO> itimsBusinessReqDTO) {
		ItimsBusinessDTO itimsBusinessDTO = itimsBusinessReqDTO.getBody().getItimsBusinessDTO();
		ItimsBusinessBean itimsBusinessBean = EntityDtoConvertUtil.convertToEntity(itimsBusinessDTO, ItimsBusinessBean.class);
		itimsBusinessBean = itimsBusinessService.getItimsBusinessDetail(itimsBusinessBean.getId());
		itimsBusinessDTO = EntityDtoConvertUtil.convertToDTO(itimsBusinessBean, ItimsBusinessDTO.class);
		ItimsBusinessResDTO itimsBusinessResDTO = new ItimsBusinessResDTO();
		itimsBusinessResDTO.setItimsBusinessDTO(itimsBusinessDTO);
		return new PaasBaseResponse<>(itimsBusinessResDTO);
	}
	
	/**
	 * 删除接口
	 * */
	@PostMapping("/deleteItimsBusiness")
	public PaasBaseResponse<ItimsBusinessResDTO> deleteItimsBusiness(@RequestBody PaasBaseRequest<ItimsBusinessReqDTO> itimsBusinessReqDTO) {
		ItimsBusinessDTO itimsBusinessDTO = itimsBusinessReqDTO.getBody().getItimsBusinessDTO();
		ItimsBusinessBean itimsBusinessBean = EntityDtoConvertUtil.convertToEntity(itimsBusinessDTO, ItimsBusinessBean.class);
		Boolean flag = itimsBusinessService.deleteItimsBusiness(itimsBusinessBean);
		ItimsBusinessResDTO itimsBusinessResDTO = new ItimsBusinessResDTO();
		return new PaasBaseResponse<>(itimsBusinessResDTO);
	}
}
